package com.hx;

import java.util.concurrent.TimeUnit;

/**
 * @author Huathy
 * @date 2021-10-08 23:13
 * @description 不能保证线程操作的原子性。存在数据安全性的问题。
 */
public class AtomicDemo {
    private static int count = 0;

    public static void incr() {
//    public synchronized static void incr() {
        synchronized (AtomicDemo.class) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> incr()).start();
        }
        Thread.sleep(5000);
        System.out.println("运行结果：" + count);
    }
}
